package com.eastfair.common.controller;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eastfair.common.entity.CommonInterfaceCallRecord;
import com.eastfair.common.service.CommonInterfaceCallRecordService;
import com.eastfair.core.context.ContextUtil;
import core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassNameRetryTask
 * @Description TODO
 * @Authoradmin
 * @Date 2022/12/27 15:59
 * @Version 1.0
 **/
@Service
@Slf4j
public class RetryTask {

    @Value("${roma.LOGISTICS-X-HW-ID}")
    private String romaXHWID;

    @Value("${roma.LOGISTICS-X-HW-APPKEY}")
    private String romaXHWAPPKEY;

    @Value("${com.eastfair.projectId}")
    private Long projectId;
    @Resource
    private CommonInterfaceCallRecordService commonInterfaceCallRecordService;

    /**
     * @Author clm
     * @Description //重试任务
     * @Date 16:26 2022/12/27
     * @Param []
     * @return void
     **/
    @XxlJob("retryTask")
    public void retryTask(){
        ContextUtil.setProjectId(999999999999L);
        QueryWrapper<CommonInterfaceCallRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", -1);
        queryWrapper.lt("retry_count", 5);
        queryWrapper.last("limit 5");
        List<CommonInterfaceCallRecord> list = commonInterfaceCallRecordService.list(queryWrapper);
        for (CommonInterfaceCallRecord commonInterfaceCallRecord : list){
            commonInterfaceCallRecord.setStatus(0);
        }
        commonInterfaceCallRecordService.updateBatchById(list);

        for (CommonInterfaceCallRecord commonInterfaceCallRecord : list){
            retryProcess(commonInterfaceCallRecord);
        }

    }
    public void retryProcess(CommonInterfaceCallRecord commonInterfaceCallRecord){

        try {

            HttpResponse miceHttp = HttpRequest.post(commonInterfaceCallRecord.getHttpUrl())
                    .header("X-HW-ID", romaXHWID)
                    .header("X-HW-APPKEY", romaXHWAPPKEY)
                    .contentType("application/json")
                    .body(commonInterfaceCallRecord.getHttpAttr())
                    .timeout(10000)
                    .execute();

            if (miceHttp.isOk()){
                commonInterfaceCallRecord.setStatus(1);
                String body = miceHttp.body();
                log.info(commonInterfaceCallRecord.getHttpUrl() + "++retryProcess:{}", body);
//                HttpRequest.post(commonInterfaceCallRecord.getCallbackUrl()).header(Header.USER_AGENT, "mice http").body(body).timeout(10000).execute();
            } else {
                commonInterfaceCallRecord.setStatus(-1);
            }

        }catch (Exception e){
            log.error(e.getMessage(), e);
            commonInterfaceCallRecord.setErrorMsg(e.getMessage());
            commonInterfaceCallRecord.setStatus(-1);
        }

        commonInterfaceCallRecord.setRetryCount(commonInterfaceCallRecord.getRetryCount() + 1);
        commonInterfaceCallRecordService.updateById(commonInterfaceCallRecord);
    }
}
